package com.mk.common.util;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mk.common.constants.BizEnum;
import com.mk.common.entity.DE;
import com.mk.common.entity.Person;
import com.mk.common.entity.Student;
import com.mk.common.model.BizParamDTO;

public class AD {
	private int val;
	static {
	}

	public int getVal() {
		return this.val;
	}

	@Test
	public void test1() {
		Boolean flag = false;
		if (flag = true) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}

//		Integer num = 10;
//		if(num=10) {
//			
//		}

	}

	@Test
	public void test2() {
		StatusRefITSMEnum enum1 = StatusRefITSMEnum.getEnumByChgCode("0");

//		System.out.println(enum1);
		System.out.println("业务需求状态: " + enum1.getBusReqStatus());
		System.out.println("批次需求状态: " + enum1.getBatchReqStatus());
		System.out.println("开发任务状态: " + enum1.getDevTaskStatus());
	}

	@Test
	public void test3() {
		List<String> batchReqList = new ArrayList<>();
		batchReqList.add("pc2019100039");
		batchReqList.add("pc2019100040");
		batchReqList.add("pc2019100041");
		batchReqList.add("pc2019100042");

		// ["pc2019100039","pc2019100040","pc2019100041","pc2019100042"]
		String jsonStr = JSON.toJSONString(batchReqList);
		System.out.println(jsonStr);

		List<String> res = JSON.parseArray(jsonStr, String.class);
		System.out.println(res);
	}

	@Test
	public void test4() {
		List<String> list = new ArrayList<>(4);
		list.add("pc2019100039");
		list.add("pc2019100040");
		list.add("pc2019100041");
		list.add("pc2019100042");

		JSONObject param = new JSONObject();
		param.put("batchReqList", list);

		System.out.println(param);
	}

	@Test
	public void test5() {
		String[] arr = { "pc2019100039", "pc2019100040", "pc2019100041", "pc2019100042" };
		String jsonStr = JSON.toJSONString(arr);

		System.out.println(jsonStr);
	}

	@Test
	public void test6() {
		System.out.println(BizEnum.DB.toString());
	}

	@Test
	public void test7() {
		// 测试Enum
		StatusRefITSMEnum enum2 = StatusRefITSMEnum.valueOf("SUCCESS");
		System.out.println(enum2); // SUCCESS
	}

	@Test
	public void test8() {
		StatusRefITSMEnum enum3 = StatusRefITSMEnum.valueOf(StatusRefITSMEnum.class, "itsmImplCode");
		System.out.println(enum3);

//		StatusRefITSMEnum.
	}

	@Test
	public void test9() {
		Long va = 478L;
		Long vb = 478L;
		Long vc = null;
		String vd = "478";

		System.out.println(va == vb); // false
		System.out.println(va.longValue() == vb); // true
//		System.out.println(va.longValue()==vc); //NullPointerException
		System.out.println(va.equals(vc)); // false
		System.out.println(va.equals(vb)); // true
		System.out.println(va.equals(vd)); // false
	}

	@Test
	public void test11() {
		String a = Optional.ofNullable(null).toString();
		System.out.println(a); // Optional.empty

		System.out.println(Optional.ofNullable(null).isPresent()); // false
	}

	@Test
	public void test12() {
		String val = Optional.ofNullable("").orElse("默认。。。");
		System.out.println(val);
	}

	@Test
	public void test13() {
		String val = StatusRefITSMEnum.valMap.get("0").getDevTaskStatus();
		System.out.println(val);
	}

	@Test
	public void test14() {
		Optional.ofNullable(null).orElseThrow(() -> new RuntimeException("垃圾。。"));
	}

	@Test
	public void test15() {
		List<String> list = null;
		List<String> res = Optional.ofNullable(list).orElseGet(() -> Collections.emptyList());
		System.out.println(JSON.toJSONString(res));
	}

	public List<String> findRecords(BizParamDTO param) {
		BizParamDTO bizParamDTO = Optional.ofNullable(param).orElse(new BizParamDTO());
		Optional.ofNullable(param).ifPresent(p -> Collections.emptyList());
		return null;
	}

	@Test
	public void test16() {
		String param1 = null, parm2 = "", param3 = null;
		String val = Optional.ofNullable(param1).ofNullable(parm2).ofNullable(param3).orElse("默认值");
		System.out.println(val); // 默认值
	}

	/**
	 * TODO
	 */
	@Test
	public void test17() {
		// √√
		List<String> pids = Arrays.asList("1", "2", "3", "d");
//		 = Collections.emptyList();
		Optional.ofNullable(pids).filter(t -> !t.isEmpty()).ifPresent(t -> {
			t.forEach(System.out::println);
		});
	}

	@Test
	public void test18() {
		List<String> pids = Collections.emptyList();
		Optional.ofNullable(pids).filter(t -> !t.isEmpty()).orElseGet(() -> new ArrayList());
	}

	@Test
	public void test19() {
		BizParamDTO param = new BizParamDTO();
		param.setBizType(BasicConstants.BIZ_LSJJ);
		param.setUserId("1820");
//		param.setEventId("fsahsd237842hd632642ehe824892");
		Optional<BizParamDTO> optional = Optional.ofNullable(param).filter(t -> !StringUtils.isEmpty(t.getEventId()));
//		System.out.println(optional.isPresent()); // false

		optional.ifPresent(t -> System.out.println(t));
		optional.orElseGet(() -> {
			System.out.println("事件id为空！");
			return param;
		});
	}

	@Test
	public void test20() {
		BizParamDTO param = new BizParamDTO();
		param.setBizType(BasicConstants.BIZ_LSJJ);
		param.setUserId("1820");
//		param.setEventId("fsahsd237842hd632642ehe824892");

		Optional<String> op = Optional.ofNullable(param).map(t -> t.getEventId());
		op.ifPresent(val -> System.out.println(val));
		op.orElseGet(() -> {
			return "空ID";
		});

		System.out.println(op);
	}

	@Test
	public void test21() {
		BizParamDTO param = null;
//		= new BizParamDTO();
//		param.setRid(3L);
//		param.setBizType("zida");
//		param.setPage(12);

		Optional.ofNullable(param).filter(t -> t.getPage() != null).filter(t -> t.getRid() != null).ifPresent(t -> {
			System.out.println("非空，都有值...");
		});
	}

	@Test
	public void test22() {
		double sc = +2d;
		System.out.println(String.valueOf(sc)); // 2.0
		System.out.println((long) sc); // 2
		System.out.println(sc > 0); // true
	}

	@Test
	public void test23() {
		Double val = -2.4;
		Double val2 = 104.0;
		System.out.println(val.intValue());
		System.out.println(val2.intValue());
		System.out.println(String.valueOf(Math.round(val2) - val2 == 0 ? val2.intValue() : val2)); // 104.0
		System.out.println(Math.round(val2) - val2 == 0 ? String.valueOf(val2.intValue()) : String.valueOf(val2)); // 104

	}

	@Test
	public void test24() {
		String as = "2".equals(2) ? "√" : "×";
		String as2 = 2 == new Integer(2) ? "√" : "×";
		String as3 = 2 == Integer.valueOf(2) ? "√" : "×";
		String as4 = new Integer(2) == Integer.valueOf(2) ? "√" : "×";
		String as5 = new Integer(2).equals(Integer.valueOf(2)) ? "√" : "×";
		String as6 = new Integer(2) == new Integer(2) ? "√" : "×";
		String as7 = new Integer(2).equals(new Integer(2)) ? "√" : "×";
		System.out.println(as); // ×
		System.out.println(as2); // √
		System.out.println(as3); // √
		System.out.println(as4); // ×
		System.out.println(as5); // √
		System.out.println(as6); // ×
		System.out.println(as7); // √
	}

	@Test
	public void test25() {
		Double deduct = 10.0;
		System.out.println(deduct.doubleValue() == deduct.intValue() ? deduct.intValue() : deduct.doubleValue()); // 10.0

		String res = deduct.doubleValue() == deduct ? String.valueOf(deduct.intValue()) : String.valueOf(deduct);
		System.out.println(res); // 10
	}

	@Test
	public void test26() {
		BigDecimal d = new BigDecimal("10.0");
		Object a = d.doubleValue() == d.intValue() ? d.intValue() : d.doubleValue();
		String sre = String.valueOf(d.doubleValue() == d.intValue() ? d.intValue() : d.doubleValue());
		System.out.println(a);
	}

	@Test
	public void test27() {
		List<Integer> yearList = new ArrayList<>(3);
		for (int i = 3; i >= 0; i--) {
			yearList.add(2023 - i);
		}

		System.out.println(yearList);
	}

	@Test
	public void test28() {
		Integer[][] quarterWithMonth = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
		Date today = new Date();
		Integer currMonth = DateUtils.toCalendar(today).get(Calendar.MONTH) + 1; // 当前月份

		List<String> scoreList = new ArrayList<>();
		for (Integer[] monthArr : quarterWithMonth) {
			Integer m = monthArr[2];
//        	System.out.println(m);
			if (currMonth <= m) {
				scoreList.add("0");
				continue;
			}
			scoreList.add("79");
		}

		scoreList.forEach(System.out::println);
	}

	@Test
	public void test29() {
		for (int i = 1; i <= 12; i++) {
			System.out.println((i + 2) / 3);
		}
	}

	@Test
	public void test30() {
		Integer ye = 2023;
		Integer ty = 2023;

		System.out.println(ye == ty); // false
	}

	@Test
	public void test31() {
		int ye = 2023;
		Integer ty = 2023;

		System.out.println(ye == ty); // true
	}

	@Test
	public void test32() {
		Person p = new Person();
		p.setPersonName("着地");

		Person p2 = new Person();
		p.setPersonName("和假货");

		List<Person> list = Arrays.asList(p, p2);
		String jsonStr = JSON.toJSONString(list);
		Map<String, Object> data = JSON.parseObject(jsonStr, Map.class);

		System.out.println(data); // 报错， ×
	}

	@Test
	public void test33() {
		for (int i = 1; i <= 5; i++) {
			System.out.println(i);
		}
	}


	@Test
	public void testConnectSQLServer() {
		String USER = "sa";
		String PWD = "sa";
		String URL = "jdbc:sqlserver://10.98.215.9:1433;DatabaseName=bslis";
		String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			Class.forName(DRIVER);
			con = DriverManager.getConnection(URL, USER, PWD);
			System.out.println(con);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("连接失败！");
		}
	}
	
	@Test
	public void testConnectOracle() {
		String USER = "PORTAL56_LIS";
		String PWD = "PORTAL56_LIS";
//		String URL = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.98.208.9)(PORT=1521)))";
		String URL = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.98.208.9)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=bshis)))";
		String DRIVER = "oracle.jdbc.driver.OracleDriver";
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			Class.forName(DRIVER);
			con = DriverManager.getConnection(URL, USER, PWD);
			System.out.println(con);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("连接失败！");
		}
	}
	
	@Test
	public void TestFormatStringAndFloat() {
//		float as = Float.parseFloat(String.valueOf("3.50"));
//		System.out.println(String.format("%.0f", 3.50)); // 4
//		System.out.println(String.format("%.0f", 3.60)); // 4
//		System.out.println(String.format("%.0f", 3.30)); // 3
//		System.out.println(String.format("%.0f", 3.10)); // 3
		
		System.out.println(String.format("%.2f", 3.50)); // 3.50
		System.out.println(String.format("%.2f", 3.60)); // 3.60
		System.out.println(String.format("%.2f", 3.30)); // 3.30
		System.out.println(String.format("%.2f", 3.10)); // 3.10
	}
	
	@Test
	public void testFirstDayOfWeek() throws ParseException {
		Date da = DateUtils.parseDate("2023-09-17", "yyyy-MM-dd");
//		int res = DateUtils.toCalendar(da).getFirstDayOfWeek();
		int r3 = DateUtils.toCalendar(da).get(Calendar.DAY_OF_WEEK);
//		System.out.println(res);
		System.out.println(r3);
	}
	
	@Test
	public void testStringTOList() {
		String str = "";
		List<String> list = Stream.of(str.split(",")).collect(Collectors.toList());
		System.out.println(list);
		System.out.println(list.size()); //1
		
	}
	
	
	@Test
	public void testNumCalc() {
		// 207, 226, 243
		int red = 207, green = 226, blue = 243;
		System.out.println((byte) red + ", " + (byte) green + ", " + (byte) blue);
	}
	
	@Test
	public void testPropertyNull() {
		DE d = new DE();
		d.setName("何沐春风");
		d.setId("378273iehwqjidnkj");
		
		System.out.println(Objects.equals(d.getAge(), 12));
		System.out.println(Objects.equals(12, 12));
		
//		System.out.println( d.getAge()!=12 ); // ×， java.lang.NullPointerException
		
	}
	
	
	@Test 
	public void testEntity () {
		Long rid = 0L; 
		
		System.out.println( Objects.equals(rid, 0) ); // false
		System.out.println( Objects.equals(rid, 0L) ); // true
	}
	
	
	
	
}
